<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 上面tabs栏 -->
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="first">
          <!-- 新增角色按钮 -->
          <el-row style="height:50px">
            <el-button size="small" type="primary" @click="addRole">新增角色</el-button>
          </el-row>
          <!-- 弹窗 -->
          <el-dialog :title="title" :visible="showDialog" width="40%" @close="dialogClose">
            <!-- 新增角色表单 -->
            <el-form ref="formRef" :model="roleForm" :rules="formRules">
              <el-form-item label="角色名称" prop="name" label-width="120px">
                <el-input v-model="roleForm.name" />
              </el-form-item>
              <el-form-item label="角色描述" prop="description" label-width="120px">
                <el-input v-model="roleForm.description" />
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogClose">取 消</el-button>
              <el-button type="primary" @click="submit">确 定</el-button>
            </span>
          </el-dialog>
          <!-- <el-dialog title="新增角色" :visible="showDialog" @close="dialogClose" /> -->
          <!-- 角色列表 -->
          <el-table :data="roleList" style="width: 100%">
            <el-table-column type="index" label="序号" align="center" width="120" />
            <el-table-column prop="name" label="角色" align="center" width="240" />
            <el-table-column prop="description" label="描述" align="center" />
            <el-table-column label="操作" align="center" width="240" fixed="right">
              <template #default="scoped">
                <el-button size="small" type="success" @click="assignOpen(scoped.row)">分配权限</el-button>
                <el-button size="small" type="primary" @click="edit(scoped.row)">编辑</el-button>
                <el-button size="small" type="danger" @click="delRole(scoped.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分配权限弹窗 -->
          <assign-permission
            :visible="visible"
            :role-id="roleId"
            @dialog-close="assignClose"
          />

        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">公司信息</el-tab-pane>

      </el-tabs>
      <!-- 分页 -->
      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <el-pagination
          :current-page="params.page"
          :page-size="params.pagesize"
          :total="total"
          layout="prev,pager,next"
          hide-on-single-page
          @current-change="currentChangeFn"
        />
      </el-row>
    </div>
  </div>

</template>

<script>
import { getRoleList, addRole, getRoleDetail, editRole, delRole } from '@/api'
import assignPermission from './components/assign-permission.vue'
export default {
  components: { assignPermission },
  data() {
    return {
      roleId: '',
      // 表单内容
      roleForm: {
        name: '',
        description: ''
      },
      formRules: {
        name: [
          { required: true, message: '角色名称不能为空哦', trigger: 'blur' }
        ]
      },
      // 显示弹窗内容
      showDialog: false,
      // 显示权限弹窗
      visible: false,
      activeName: 'first',
      // 角色列表数据
      roleList: [],
      // 分页参数
      params: {
        page: 1,
        pagesize: 10
      },
      // 总条数
      total: 0,
      // 当前dom
      currentNode: ''
    }
  },
  computed: {
    title() {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    }
  },
  async created() {
    // const res = await getRoleList(this.params)
    // console.log(res)
    this.fetchRoleList()
  },
  methods: {
    // 打开权限弹窗
    assignOpen(row) {
      this.visible = true
      this.roleId = row.id
    },
    // 关闭权限弹窗
    assignClose() {
      this.visible = false
    },
    // 删除角色
    delRole(val) {
      this.$confirm('确定删除该角色吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 2. 调用接口
        await delRole(val.id)
        // 3. 提示用户
        this.$message.success('删除角色成功')
        // 重新获取列表
        this.fetchRoleList()
        // 最后一页最后一条数据时，页数减 1
        if (this.roleList.length === 1 && this.params.page > 1) {
          this.params.page--
        }
      }).catch(() => {
        this.$message.error('已取消删除操作')
      })
    },
    // 编辑角色
    async edit(val) {
      this.currentNode = val
      const { data } = await getRoleDetail(val.id)
      this.roleForm = data
      this.showDialog = true
    },
    // 提交表单
    submit() {
      // 表单兜底校验
      this.$refs.formRef.validate(async(valid) => {
        if (!valid) return
        if (this.roleForm.id) {
          await editRole(this.roleForm)
        } else {
          await addRole(this.roleForm)
        }
        // 提示用户
        this.$message.success(this.title + '成功')
        // 关闭弹窗
        this.dialogClose()
        // 刷新页面
        this.fetchRoleList()
      })
    },
    // 添加角色
    addRole() {
      this.showDialog = true
    },
    // 弹窗关闭
    dialogClose() {
      this.showDialog = false
      // 重置表单
      this.$refs.formRef.resetFields()
      // 清除内容
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 获取角色列表
    async fetchRoleList() {
      const { data: { rows, total }} = await getRoleList(this.params)
      this.roleList = rows
      console.log(rows)
      this.total = total
    },
    // 切换页
    currentChangeFn(val) {
      this.params.page = val
      this.fetchRoleList()
    }
  }

}
</script>

<style scoped>
.el-form-item {
  width: 90%;
}
</style>
